package com.aries.ui.widget.progress;


import com.aries.ui.widget.BasisDialog;
import com.aries.ui.util.FindViewUtil;
import com.aries.ui.view.title.ColorStateList;
import com.aries.ui.widget.ResourceTable;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.utils.TextTool;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

/**
 * @Author: AriesHoo on 2018/7/19 10:43
 * @E-Mail: AriesHoo@126.com
 * Function:UIProgress Dialog模式重构
 * Description:
 * 1、新增progressBar颜色值设置
 */
public class UIProgressDialog extends BasisDialog<UIProgressDialog> {

    private static AnimatorValue sAnimator;

    public UIProgressDialog(Context context) {
        super(context);
        setAlignment(LayoutAlignment.CENTER);
    }

    @Override
    public void hide() {
        super.hide();
        if (sAnimator != null) {
            sAnimator.stop();
            sAnimator.release();
        }
    }

    public final static int NOMAL = 0;
    public final static int CUSTOMIZE = 1;

    public interface ICreateContentView {
        Component createProgressView(int type);

        int getGravity();

        int getOrientation();
    }

    /**
     * 获取Message控件
     *
     * @return Message控件
     */
    public Text getMessage() {
        return FindViewUtil.getTargetView(mContentView, ResourceTable.Integer_tv_messageProgressDialog);
    }

    /**
     * Material 风格
     */
    public static class MaterialBuilder extends Builder<MaterialBuilder> {

        //        private CircularProgressView mProgressBar;
        private MaterialProgressBar mProgressBar;
        private int mDuration = 800;
        private boolean mRoundEnable;
        private float mBorderWidth = 6;

        public MaterialBuilder(Context context) {
            super(context);
            setTextColor(mLoadingColor).setBorderWidth(dp2px(3));
        }

        /**
         * 设置转动速度
         *
         * @param duration duration
         * @return MaterialBuilder
         */
        public MaterialBuilder setDuration(int duration) {
            mDuration = duration;
            return this;
        }

        /**
         * 设置是否圆角
         *
         * @param enable enable
         * @return MaterialBuilder
         */
        public MaterialBuilder setRoundEnable(boolean enable) {
            this.mRoundEnable = enable;
            return this;
        }

        /**
         * 设置弧度粗细
         *
         * @param w w
         * @return MaterialBuilder
         */
        public MaterialBuilder setBorderWidth(float w) {
            this.mBorderWidth = w;
            return this;
        }

        @Override
        public Component createProgressView(int type) {
            mProgressBar = new MaterialProgressBar(mContext);
//            mProgressBar = new CircularProgressView(mContext);

            mProgressBar.setLayoutConfig(new ComponentContainer.LayoutConfig(mLoadingSize, mLoadingSize));
            mProgressBar.setDuration(mDuration).setRoundEnable(mRoundEnable).setBorderWidth(mBorderWidth).setArcColor(new Color(mLoadingColor));
            return mProgressBar;
        }
    }

    /**
     * 微博风格
     */
    public static class WeBoBuilder extends ProgressBarBuilder<WeBoBuilder> {

        public WeBoBuilder(Context context) {
            super(context);
            setIndeterminateDrawable(ResourceTable.Media_img_loading_wei_bo).setLoadingSize(dp2px(30)).setMinWidth(dp2px(150)).setMinHeight(dp2px(100)).setPadding(dp2px(10)).setTextColorResource(ResourceTable.Color_colorLoadingTextWeiBo);
        }

        @Override
        public int getOrientation() {
            return DirectionalLayout.VERTICAL;
        }
    }

    /**
     * 类微信模式
     */
    public static class WeChatBuilder extends ProgressBarBuilder<WeChatBuilder> {
        public WeChatBuilder(Context context) {
            super(context);
            setIndeterminateDrawable(ResourceTable.Media_img_loading_we_chat).setTextColorResource(ResourceTable.Color_colorLoadingTextWeiBo).setBackgroundResource(mResourceUtil.getColor(ResourceTable.Color_colorLoadingBgWei));
        }
    }

    /**
     * 标准模式
     */
    public static class NormalBuilder extends ProgressBarBuilder<NormalBuilder> {
        public NormalBuilder(Context context) {
            super(context);
        }
    }

    /**
     * 系统默认ProgressBar Builder
     *
     * @param <T>
     */
    private static class ProgressBarBuilder<T extends ProgressBarBuilder> extends Builder<T> {
        private Element mIndeterminateDrawable;
        private RoundProgressBar mProgressBar;
        private Image mProgressBarIma;

        public ProgressBarBuilder(Context context) {
            super(context);
        }

        public T setIndeterminateDrawable(Element drawable) {
            mIndeterminateDrawable = drawable;
            return backBuilder();
        }

        public T setIndeterminateDrawable(int resId) {
            return setIndeterminateDrawable(mResourceUtil.getImageDrawable(resId));
        }

        @Override
        public Component createProgressView(int type) {
            if (type != 0) {
                return loading(mIndeterminateDrawable);
            } else {
                mProgressBar = new RoundProgressBar(mContext);
                mProgressBar.setLayoutConfig(new ComponentContainer.LayoutConfig(mLoadingSize, mLoadingSize));
                mProgressBar.setIndeterminate(true);
                mProgressBar.setProgressWidth(5);
                return mProgressBar;
            }

        }

        public Component loading(Element drawable) {
            try {
                mProgressBarIma = new Image(mContext);
//                Resource resource = mContext.getResourceManager().getResource(imaResource);
//                PixelMapElement element = new PixelMapElement(resource);
//                drawable.setBounds(0, 0, mLoadingSize, mLoadingSize);
                mProgressBarIma.setImageElement(drawable);
                mProgressBarIma.setLayoutConfig(new ComponentContainer.LayoutConfig(mLoadingSize, mLoadingSize));
//                ImageInfo info = mProgressBarIma.getPixelMap().getImageInfo();

                drawable.setBounds(0, 0, mLoadingSize, mLoadingSize);

//                ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(dp2px(20), dp2px(20));
//                config.setMarginRight(dp2px(10));


                sAnimator = new AnimatorValue();
                sAnimator.setDuration(1500);
                sAnimator.setLoopedCount(Animator.INFINITE);
                sAnimator.setValueUpdateListener((animatorValue, v) -> {
                    mProgressBarIma.setRotation(v * 360);
                });
                sAnimator.start();
            } catch (Exception e) {

            }

            return mProgressBarIma;
        }
    }


    /**
     * 基础Builder
     *
     * @param <T>
     */
    private static abstract class Builder<T extends Builder> extends BasisBuilder<T> implements ICreateContentView {

        protected int mLoadingSize;

        protected Text mTvMessage;
        protected CharSequence mMessageStr;
        protected ColorStateList mTextColor;
        protected float mTextSize = 14;
        protected int mTextPadding = 16;
        protected int mLoadingColor;
        private DependentLayout mLLayout;

        public void setTransparent(boolean transparent) {
            isTransparent = transparent;
        }

        private boolean isTransparent;

        public Builder(Context context) {
            super(context);
            mLoadingColor = Color.getIntColor("#546e7a");
            setBackgroundResource(mResourceUtil.getColor(ResourceTable.Color_colorLoadingBg)).setLoadingSize(dp2px(30)).setTextColorResource(ResourceTable.Color_colorLoadingText).setMinWidth(dp2px(200)).setMinHeight(dp2px(70)).setBackgroundRadiusResource(ResourceTable.String_dp_radius_loading).setPadding(dp2px(10));
        }


        /**
         * 设置弧度颜色
         *
         * @param color color
         * @return backBuilder
         */
        public T setLoadingColor(int color) {
            mLoadingColor = color;
            return backBuilder();
        }

        /**
         * setLoadingColorResource
         *
         * @param res res
         * @return setLoadingColor
         */
        public T setLoadingColorResource(int res) {
            return setLoadingColor(mResourceUtil.getColor(res));
        }

        /**
         * 设置Loading 宽高
         *
         * @param size size
         * @return  backBuilder
         */
        public T setLoadingSize(int size) {
            this.mLoadingSize = size;
            return backBuilder();
        }

        /**
         * 设置标题
         * {@link Text#(CharSequence)}
         *
         * @param charSequence charSequence
         * @return backBuilder
         */
        public T setMessage(CharSequence charSequence) {
            this.mMessageStr = charSequence;
            return backBuilder();
        }

        public T setMessage(int resId) {
            return setMessage(mResourceUtil.getText(resId));
        }

        /**
         * 设置标题文字颜色
         * {@link Text#(ColorStateList)}
         *
         * @param color color
         * @return backBuilder
         */
        public T setTextColor(ColorStateList color) {
            this.mTextColor = color;
            return backBuilder();
        }

        public T setTextColor(int color) {
            return setTextColor(ColorStateList.valueOf(color));
        }

        public T setTextColorResource(int resId) {
            return setTextColor(mResourceUtil.getColor(resId));
        }

        public T setTextSize(float size) {
            this.mTextSize = size;
            return backBuilder();
        }

        /**
         * 设置标题文本尺寸
         * {@link Text#(int, float)}
         *
         * @param unit unit
         * @param size size
         * @return backBuilder
         */
        public T setTextSize(Text.TextSizeType unit, float size) {
            this.mTextSize = size;
            this.mTextSizeUnit = unit;
            return backBuilder();
        }

        /**
         * 设置文本padding
         *
         * @param padding padding
         * @return backBuilder
         */
        public T setTextPadding(int padding) {
            this.mTextPadding = padding;
            return backBuilder();
        }

        public UIProgressDialog create(int type) {
//            int margin = dp2px(12);
            Component contentView = createContentView(type);
            mDialog = new UIProgressDialog(mContext);
            mDialog.setContentView(contentView);
            mDialog.setAlignment(LayoutAlignment.CENTER);
            mDialog.setTransparent(true);

            WindowManager.LayoutConfig config = new WindowManager.LayoutConfig();
            config.width = ComponentContainer.LayoutConfig.MATCH_PARENT;
            config.height = ComponentContainer.LayoutConfig.MATCH_PARENT;
            mDialog.setAttributes(config);
            mDialog.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
            setDialog();
            return (UIProgressDialog) mDialog;
        }

        private Component createContentView(int type) {

            mLLayout = new DependentLayout(mContext);


            DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
            mLLayout.setLayoutConfig(layoutConfig);
//            mDialog.setGravity(mGravity);
            mLLayout.setLayoutConfig(new DependentLayout.LayoutConfig(DependentLayout.LayoutConfig.MATCH_PARENT, DependentLayout.LayoutConfig.MATCH_PARENT));
//            ShapeElement element = new ShapeElement();
//            element.setRgbColor(RgbPalette.TRANSPARENT);
//            mLLayout.setBackground(element);
            mLLayout.setAlignment(LayoutAlignment.CENTER);

            ShapeElement element1 = new ShapeElement();
            if (isTransparent) {
                element1.setRgbColor(RgbPalette.WHITE);
            } else {
                element1.setRgbColor(RgbColor.fromArgbInt(0x66ffffff));
            }

            mLLayoutRoot = new DirectionalLayout(mContext);
            mLLayoutRoot.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    return;
                }
            });
            mLLayoutRoot.setId(ResourceTable.Integer_lLayout_rootProgressDialog);
            mLLayoutRoot.setAlignment(getGravity());
            mLLayoutRoot.setOrientation(getOrientation());
            mLLayoutRoot.setBackground(element1);

            setRootView();
            mLLayout.removeAllComponents();

            mLLayoutRoot.addComponent(createProgressView(type));
            createText();
            mLLayout.addComponent(mLLayoutRoot);
            return mLLayout;
        }

        private void createText() {
            if (TextTool.isNullOrEmpty(mMessageStr)) {
                mLLayoutRoot.setOrientation(DirectionalLayout.HORIZONTAL);
//                mLLayoutRoot.setMinWidth(dp2px(55));
//                mLLayoutRoot.setMinHeight(dp2px(65));
                return;
            }
            mLLayoutRoot.setMinWidth(mMinWidth);
            mLLayoutRoot.setMinHeight(mMinHeight);

            mTvMessage = new Text(mContext);
            mTvMessage.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
            mTvMessage.setId(ResourceTable.Integer_tv_messageProgressDialog);
            mTvMessage.setText(String.valueOf(mMessageStr));
            mLLayoutRoot.addComponent(mTvMessage);

            setTextViewLine(mTvMessage);

            setTextAttribute(mTvMessage, String.valueOf(mMessageStr), mTextColor, (int) mTextSize, TextAlignment.LEFT, false);
            mTvMessage.setPadding(mTextPadding, mTextPadding, mTextPadding, mTextPadding);

        }

        @Override
        public int getGravity() {
            return LayoutAlignment.CENTER;
        }

        @Override
        public int getOrientation() {
            return DirectionalLayout.HORIZONTAL;
        }
    }

}
